package cn.gson.school.controller.jiaoxue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiaoxue.PhaseDao;
import cn.gson.school.dao.jiaoxue.ZhangJieDao;
import cn.gson.school.dao.xitong.UserInfoDao;
import cn.gson.school.entity.jiaoxue.ChapterEntity;
import cn.gson.school.entity.jiaoxue.PhaseEntity;
import cn.gson.school.entity.xitong.UserInfoEntity;
import cn.gson.school.mappers.jiaoxue.PhaseMappers;

@Controller
@RequestMapping("/phase")
public class PhaseController {

	@Autowired
	private PhaseMappers phaseMappers;

	@Autowired
	private PhaseDao phaseDao;

	@Autowired
	private UserInfoDao userinfoDao;
	
	@Autowired
	private ZhangJieDao zhangjieDao;

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> phaseList(PhaseEntity phaseEntity,
			@RequestParam(name = "page", defaultValue = "1") int pageNum,
			@RequestParam(name = "rows", defaultValue = "20") int pageSize) {
		// 查询数据
		PageHelper.startPage(pageNum, pageSize);
		List<PhaseEntity> list = phaseMappers.phaseList(phaseEntity);
		PageInfo<PhaseEntity> info = new PageInfo<>(list);
		// 组装返回结果
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}

	@PostMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> addPhase(@Valid PhaseEntity phase, HttpServletRequest request, Model model,
			BindingResult br) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("errors", br.getFieldErrors());
		} else {
			String jieduanName = request.getParameter("jieduanName");
			String phaseId = request.getParameter("phaseId");
			System.out.println("阶段编号：" + phaseId);
			PhaseEntity phaseEntity = null;
			if (phaseId == null || "".equals(phaseId)) {// 进行添加
				System.out.println("添加");
				String userId = request.getParameter("userinfo");// 获取当前的操作人
				UserInfoEntity userInfo = userinfoDao.findOne(Long.valueOf(userId));
				phaseEntity = new PhaseEntity();// 新建一个
				phaseEntity.setUserinfoId(userInfo);
				phaseEntity.setPhaseName(jieduanName);
			} else {// 进行编辑
				System.out.println("编辑");
				phaseEntity = phaseDao.findOne(Long.valueOf(phaseId));
				phaseEntity.setPhaseId(Long.valueOf(phaseId));
				System.out.println(jieduanName);
				phaseEntity.setPhaseName(jieduanName);
			}

			phaseEntity.setAddingTime(new Date());
			phaseEntity.setDelete(1);
			phaseDao.save(phaseEntity);
			obj.put("success", true);
		}
		return obj;
	}

	@GetMapping("delete/{id}")
	@ResponseBody
	// 指定使用jpa的事务管理器
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		PhaseEntity phaseEntity = phaseDao.findOne(id);
		if (id != null && (phaseEntity) != null) {
			//删除阶段下的章节	
			List<ChapterEntity> chapter=zhangjieDao.findByPhaseId(id);
			for (ChapterEntity chapterEntity : chapter) {
				chapterEntity.setDelete(0);
			}
			zhangjieDao.save(chapter);
			//删除阶段
			phaseEntity.setDelete(0);
			phaseDao.save(phaseEntity);
			obj.put("success", true);
		} else {
			obj.put("message", "你要删除的阶段课程不存在！");
		}
		return obj;
	}
}
